<?php
namespace Home\Controller;

use Think\Controller;

/**
 * 入口城市管理
 * @author DONGGUIYU
 *
 */
class AuthorityController extends Controller{
    /**
     * 入口城市列表
     */
    public function entryCityList(){
        // 查询条件
        // 城市ID
        $cityId = I('post.city', "", 'htmlspecialchars');
        if(intval($cityId) > 0){
            $where['city_id'] = $cityId;
            $this->assign("cityId", $cityId);
        }
        // 入口城市MODEL
        $entryCityModel = new \Home\Model\EntryCityModel();
        // 分页
        $page = setPage($entryCityModel, $where);
        // 进行分页数据查询
        $orderby['id']='asc';
        // 入口城市
        if(isset($where)){
            $entryCityList = $entryCityModel->relation(true)->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        } else {
            $entryCityList = $entryCityModel->relation(true)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        }
        $show = $page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("entryCityList", $entryCityList);
        // 城市
        $cityModel = D('City');
//         unset($where);
//         $where['parent_id'] = array('gt', 0);
//         $cityList = $cityModel->where($where)->select();
        $cityList = $cityModel->select();
        $this->assign("cityList", $cityList);
        $this->display('entrycity');
    }
    
    /**
     * 添加入口城市
     */
    public function addEntryCity(){
        $this->assign('title','添加入口城市');
        // 城市
        $cityModel = D('City');
//         unset($where);
//         $where['parent_id'] = array('gt', 0);
//         $cityList = $cityModel->where($where)->select();
        $cityList = $cityModel->select();
        $this->assign("cityList", $cityList);
        $this->display('editentrycity');
    }
    
    /**
     * 编辑入口城市
     */
    public function editEntryCity(){
        $this->assign('title','修改入口城市');
        // 入口城市ID
        $id = I('get.id', "", 'htmlspecialchars');
        // 入口城市model
        $entryCityModel = D('EntryCity');
        $where['id'] = $id;
        $rlt = $entryCityModel->where($where)->select();
        if(isset($rlt)){
            $this-> assign('entryCity', $rlt[0]);
        }
        // 城市
        $cityModel = D('City');
//         unset($where);
//         $where['parent_id'] = array('gt', 0);
//         $cityList = $cityModel->where($where)->select();
        $cityList = $cityModel->select();
        $this->assign("cityList", $cityList);
        $this->display('editentrycity');
    }
    
    /**
     * 入口城市是否已经存在
     */
    public function hasEntryCity(){
        // 城市ID
        $cityId = I('post.city', "", 'htmlspecialchars');
        // 入口城市model
        $entryCityModel = D('EntryCity');
        if(!empty($cityId)){
            $where['city_id'] = $cityId;
            $rlt = $entryCityModel->where($where)->select();
            if(isset($rlt)){
                // 入口城市已存在
                echo "false";
            } else {
                // 入口城市不存在
                echo "true";
            }
        }else{
            echo "";
        }
    }
    
    /**
     * 保存入口城市
     */
    public function saveEntryCity(){
        // 入口城市ID
        $id =  I('post.entryCityId', "", 'htmlspecialchars');
        // 城市ID
        $cityId = I('post.city', "", 'htmlspecialchars');
    
        // 入口城市model
        $entryCityModel = D('EntryCity');
        $data['city_id'] = $cityId;
        $entryCityModel->startTrans();
        if(empty($id)){
            // 添加入口城市
            $rlt = $entryCityModel->data($data)->add();
        } else {
            // 修改入口城市
            $where['id'] = $id;
            $rlt = $entryCityModel->where($where)->save($data);
        }
        if($rlt != 0 && $rlt == false){
            $entryCityModel->rollback();
            $this->error("保存失败！");
            exit;
        } else {
            $entryCityModel->commit();
            $this->success("保存成功！", "entryCityList");
            exit;
        }
    }
    
    /**
     * 删除入口城市
     */
    public function deleteEntryCity(){
        $idList = I('post.idList', "", 'htmlspecialchars');
        // 入口城市model
        $entryCityModel = D('EntryCity');
        $entryCityModel->startTrans();
        if(!empty($idList)){
            $idArray = split(',', $idList);
            // 开启事务
            $entryCityModel->startTrans();
            foreach ($idArray as $id){
                $where['id'] = $id;
                $rlt = $entryCityModel->where($where)->delete();
                if($rlt == false){
                    // 删除失败时回滚事务
                    $entryCityModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
            }
            // 提交事务
            $entryCityModel->commit();
            $this->success("删除成功！", 'entryCityList');
        }
    }
    
}

?>